package com.wedo.web;/**
 * Created by cheng on 2018/4/12.
 */

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cheng
 * @create 2018/4/12
 */
@Controller
@RequestMapping("/getUrl")
public class GetUrl {
    @RequestMapping("/goJsp")
    public void getUrl(HttpServletResponse response){
        PrintWriter out = null ;
        try {
            /*String keys= HttpUtils.post("http://lotto.sina.cn/ai","");*/
            Document doc = Jsoup.parse(new URL("http://lotto.sina.cn/ai").openStream(),"UTF-8", "http://lotto.sina.cn/ai") ;
            /*Document doc = Jsoup.parse(keys) ;*/
            Element element = doc.selectFirst("#bannerSection") ;
            Element notice = doc.selectFirst("#notice") ;
            Element header = doc.selectFirst("#header") ;
            Element menu = doc.selectFirst("#ct0_header_more") ;
            Element section = doc.selectFirst("#discountSection") ;
            element.remove() ;
            notice.remove() ;
            header.remove() ;
            menu.remove() ;
            section.attr("style", "display: none") ;
            List<Element> script = doc.select("script") ;    //数据分析
            for (int i = 0; i < script.size(); i++) {
                Pattern pattern= Pattern.compile("数据分析") ;
                Matcher matcher = pattern.matcher("abcd") ;

                script.get(i).toString().replace("数据分析","abcd") ;
            }
            Elements elements=doc.select("a[href!=#]");
            //elements.
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter() ;
            out.print(doc.toString()) ;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush() ;
            out.close() ;
        }
    }
}
